import * as TYPES from "../actionType.js"

// 存放数据
let initState = {
    listleftdata: [],//左侧导航栏数据
    listrightdata: [],//右侧内容
    listitmedetail: {},//点击信息的详情
    storeList: [], // 搜索到的店铺数据
    commodityList: [],//搜索到的商品列表
    hotInputdata: [{ id: 1, val: "面包" }, { id: 2, val: "蛋糕" }, { id: 3, val: "冰激凌" }], //热门搜索
    historydata: [],// 历史搜索
    cardata: [],//购物车数据
    choosetype: {},//获取选中的数据
    similardata: [],//类似的商品
    collection_commodity: "0",//商品收藏状态
    carNumber: 0,//购物车商品总数量
    carMoney: 0, // 购物车商品总钱数
    storeMess: [],//店铺信息
    storeCommodity: [],//该店铺商品信息
    data_purchases: [],//热榜
    data_collection: [],//心愿单
    data_recommend: [],//店长推荐
    data_types: [],//商店的店铺导航
    collection_store: "0",//店铺收藏
}

function foodF(state = initState, action) {
    let { type, payload } = action
    let { listleftdata, listrightdata, carMoney, carNumber, historydata, cardata, choosetype, similardata, storeList, commodityList,
        data_types, storeMess, storeCommodity, data_purchases, data_collection, data_recommend, collection_store
    } = state
    switch (type) {
        // 左侧导航栏数据(调取接口)
        case TYPES.FOOD_GET_LIST_LEFT_DATA:
            return { ...state, listleftdata: payload }
        // 右侧内容(调取接口)
        case TYPES.FOOD_GET_LIST_RIGHT_DATA:
            // console.log(payload);
            // commodity_type_ice  冰量
            // commodity_type_size 尺寸/口味
            // commodity_type_sugar 糖量
            // commodity_unitPrice 价钱
            // listrightdata 存放整体数据
            // 将传入的单层数据 合并为多层数据
            if (listleftdata.length > 0) {
                // console.log(payload);
                listleftdata.forEach(leftitem => {
                    let objkind = {//存放合并数据后的数据
                        shop_typeMess: "",//商品类型名
                        shop_type: [] // 所有商品种类
                    };
                    // 数组判断类型
                    // 尺寸类型 0 面包 1 冷饮 2 冰激凌 3 曲奇饼干 4 奶油蛋糕 5 宅家零食 6 热饮

                    // 查找相同类型
                    objkind.shop_type = payload.filter(rightitem => {
                        if (leftitem.type_commodity_kind === rightitem.commodity_kind) {
                            payload.forEach(item => {
                                let types = []

                                if (item.commodity_type_ice.length > 0) {
                                    let objkind = {//存放合并数据后的数据
                                        shop_typeMess: "",//商品类型名
                                        shop_type: [] // 所有商品种类
                                    };
                                    objkind.shop_typeMess = "冰量"
                                    objkind.shop_type = item.commodity_type_ice
                                    types.push(objkind)
                                }
                                if (item.commodity_type_size.length > 0) {
                                    let objkind = {//存放合并数据后的数据
                                        shop_typeMess: "",//商品类型名
                                        shop_type: [] // 所有商品种类
                                    };
                                    objkind.shop_typeMess = "尺寸/口味"
                                    objkind.shop_type = item.commodity_type_size
                                    types.push(objkind)
                                }
                                if (item.commodity_type_sugar.length > 0) {
                                    let objkind = {//存放合并数据后的数据
                                        shop_typeMess: "",//商品类型名
                                        shop_type: [] // 所有商品种类
                                    };
                                    objkind.shop_typeMess = "糖量"
                                    objkind.shop_type = item.commodity_type_sugar
                                    types.push(objkind)
                                }

                                item.types = types
                            })
                            // 数据内添加一个类的名字
                            rightitem.type_commodity_kind = leftitem.type_typeMess
                            return rightitem
                        }

                    })

                    objkind.shop_typeMess = leftitem.type_typeMess
                    // 数据整合
                    listrightdata.push(objkind);

                })
            }
            return { ...state, listrightdata: JSON.parse(JSON.stringify(listrightdata)) }
        // 存放点击的商品信息(调取接口)
        case TYPES.FOOD_JUMP_LIST_ITME_DETAIL:
            let types = []
            // if (payload.commodity.code === "01") {
            if (payload.commodity.commodity_type_ice.length > 0) {
                let objkind = {//存放合并数据后的数据
                    shop_typeMess: "",//商品类型名
                    shop_type: [] // 所有商品种类
                };
                objkind.shop_typeMess = "冰量"
                objkind.shop_type = payload.commodity.commodity_type_ice
                types.push(objkind)
            }
            if (payload.commodity.commodity_type_size.length > 0) {
                let objkind = {//存放合并数据后的数据
                    shop_typeMess: "",//商品类型名
                    shop_type: [] // 所有商品种类
                };
                objkind.shop_typeMess = "尺寸/口味"
                objkind.shop_type = payload.commodity.commodity_type_size
                types.push(objkind)
            }
            if (payload.commodity.commodity_type_sugar.length > 0) {
                let objkind = {//存放合并数据后的数据
                    shop_typeMess: "",//商品类型名
                    shop_type: [] // 所有商品种类
                };
                objkind.shop_typeMess = "糖量"
                objkind.shop_type = payload.commodity.commodity_type_sugar
                types.push(objkind)
            }

            // }

            payload.commodity.types = types
            return {
                ...state, listitmedetail: JSON.parse(JSON.stringify(payload.commodity)),
                similardata: JSON.parse(JSON.stringify(payload.data)),
                collection_commodity: payload.collection_commodity
            }
        // 添加购物车(调取接口)
        case TYPES.FOOD_GET_CAR_DATA:
            // 获取时计算购物车商品总数量和总钱数
            carMoney = 0
            carNumber = 0
            payload.forEach(item => {
                carMoney = Number(item.all_price) * 1000 + carMoney
                carNumber += Number(item.number)
            })
            return { ...state, cardata: payload, carMoney: carMoney / 1000, carNumber }
        // 计算购物车商品总数量和总钱数（商品数量的++ --）
        case TYPES.FOOD_COUNT_CAR_NUM:
            let { num, id } = payload
            // 先清零
            carMoney = 0
            carNumber = 0
            // 修改购物车商品数量 和 商品价钱
            cardata = cardata.filter(item => {
                // 判断购物车id
                if (item.car_id === id) {
                    if (num === -1) {
                        if (Number(item.number) > 0) {
                            item.number = Number(item.number) - 1
                        } else {
                            return
                        }
                    } else if (num === 1) {
                        item.number = Number(item.number) + 1
                    }
                    // 每条数据的总价格计算
                    item.all_price = ((Number(item.price) * 1000) * Number(item.number)) / 1000
                    // console.log(item.all_price);
                }
                return item
            })

            // 计算总价和总数量
            cardata.forEach(item => {
                carMoney = Number(item.all_price) * 1000 + carMoney
                carNumber += Number(item.number)
            })

            return { ...state, cardata, carMoney: carMoney / 1000, carNumber }
        // 选规格存放处
        case TYPES.FOOD_GET_CHOOSE_TYPE:
            return { ...state, choosetype: payload }
        // 已经搜索过的信息
        case TYPES.FOOD_INPUT_ACHIEVE:
            if (historydata.length > 0) {
                if (payload !== "") {
                    let index = historydata.findIndex(item => item.val === payload)
                    if (index === -1) {
                        historydata.push({ id: historydata.length + 1, val: payload })
                    }
                }
            } else {
                historydata.push({ id: 1, val: payload })
            }

            historydata = JSON.parse(JSON.stringify(historydata))
            return { ...state, historydata }
        // 搜索数据获取
        case TYPES.FOOD_INPUT_ADD:
            return { ...state, storeList: payload.storeList, commodityList: payload.commodityList }
        // 添加购物车
        case TYPES.FOOD_ADD_CAR_DATA:
            console.log(payload)
            return { ...state, cardata: payload }
        // 选规格存放处
        case TYPES.FOOD_GET_CHOOSE_TYPE:
            return { ...state, choosetype: payload }
        // 商品收藏
        case TYPES.FOOD_GET_COLLECT_COMMODITY_STATE:
            // console.log(payload);
            return { ...state, collection_commodity: payload }
        // 店铺信息
        case TYPES.FOOD_GET_STORE_DATA:
            console.log(payload);
            storeCommodity = []
            data_types = []
            if (payload.code === "01" && storeCommodity.length === 0) {
                storeCommodity.push({
                    shop_typeMess: "热销",
                    shop_type: payload.data_purchases
                })

                data_types.push({
                    type_commodity_kind:"7",
                    type_id:"0",
                    type_typeMess:"热销"
                })

                payload.type.forEach(leftitem => {
                    let objkind = {//存放合并数据后的数据
                        shop_typeMess: "",//商品类型名
                        shop_type: [] // 所有商品种类
                    };

                    // 数组判断类型
                    // 尺寸类型 0 面包 1 冷饮 2 冰激凌 3 曲奇饼干 4 奶油蛋糕 5 宅家零食 6 热饮
                    // 查找相同类型
                    objkind.shop_type = payload.storeCommodity.filter(rightitem => {
                        if (leftitem.type_commodity_kind === rightitem.commodity_kind) {
                            payload.storeCommodity.forEach(item => {
                                let types = []

                                if (item.commodity_type_ice.length > 0) {
                                    let objkind = {//存放合并数据后的数据
                                        shop_typeMess: "",//商品类型名
                                        shop_type: [] // 所有商品种类
                                    };
                                    objkind.shop_typeMess = "冰量"
                                    objkind.shop_type = item.commodity_type_ice
                                    types.push(objkind)
                                }
                                if (item.commodity_type_size.length > 0) {
                                    let objkind = {//存放合并数据后的数据
                                        shop_typeMess: "",//商品类型名
                                        shop_type: [] // 所有商品种类
                                    };
                                    objkind.shop_typeMess = "尺寸/口味"
                                    objkind.shop_type = item.commodity_type_size
                                    types.push(objkind)
                                }
                                if (item.commodity_type_sugar.length > 0) {
                                    let objkind = {//存放合并数据后的数据
                                        shop_typeMess: "",//商品类型名
                                        shop_type: [] // 所有商品种类
                                    };
                                    objkind.shop_typeMess = "糖量"
                                    objkind.shop_type = item.commodity_type_sugar
                                    types.push(objkind)
                                }

                                item.types = types
                            })
                            // 数据内添加一个类的名字
                            rightitem.type_commodity_kind = leftitem.type_typeMess
                            return rightitem
                        }

                    })

                    objkind.shop_typeMess = leftitem.type_typeMess

                    // 数据整合
                    storeCommodity.push(objkind);
                })
            }

            data_types.push(...payload.type)

            console.log(data_types);

            return {
                ...state,
                storeMess: payload.storeMess,
                storeCommodity: JSON.parse(JSON.stringify(storeCommodity)),
                data_purchases: payload.data_purchases,
                data_collection: payload.data_collection,
                data_recommend: payload.data_recommend,
                collection_store: payload.collection_store,
                data_types: JSON.parse(JSON.stringify(data_types))
            }
        // 店铺关注
        case TYPES.FOOD_CHANGE_STORE_COLLECT:
            return { ...state, collection_store: payload }
        // 跳转路由清除当前搜索记录
        case TYPES.FOOD_INPUT_CLEAR:
            return { ...state, storeList: payload, commodityList: payload }
        // 清空历史搜索
        case TYPES.FOOD_HISTORY_INPUT_CLEAR:
            return { ...state, historydata: payload }

    }
    return state
}

export default foodF
